package solitaire.deck;

import solitaire.extras.*;
import java.awt.Graphics;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.*;
import java.util.ArrayList;

import javax.smartcardio.CardTerminal;

public class Pile extends DrawObject {

	public enum PileType {CARDS_POSITION_ONTOP, CARDS_POSITION_BELOW}
	
	// Store all the cards for this pile.
	private CardCollection cards = new CardCollection(); 
	private PileType pileType;
	
	public Pile(PileType type)
	{
		pileType = type;
	}
	
	// Get the cards on this pile
	public CardCollection getCards()
	{
		return cards;
	}
	
	//Draws a rectangle on the screen
	public void drawObject(Graphics g)
	{
		double y = this.m_dStartY;
		
		if(y < 100)
		{
		g.setColor(Color.BLUE);
		g.drawRect( 	
				(int)(m_dStartX - m_dSidelength /2), 
				(int)(m_dStartY - m_dSidelength /2), 
				(int)m_dSidelength, getPileLength() + 35);
		}

		for (Card c : cards)
		{
			c.drawObject(g);
		}
	}
	
	//Returns true if the given point where the pile is positioned
	public boolean contains(Point2D point)
	{
		return new Rectangle2D.Double( m_dStartX - m_dSidelength /2, 
				m_dStartY - m_dSidelength /2, m_dSidelength, this.getPileLength() ).contains( point );
	}
	
	// Set a new location for the pile
	public void setLocation(double dX, double dY)
	{
		m_dStartX = dX;
        m_dStartY = dY;
	}
	
	public int getPileLength()
	{
		if(pileType == PileType.CARDS_POSITION_BELOW)
		{
			return 95 + 30 * ((cards.size() == 1) ? 0 : cards.size()-1);
		}
		else
		{
			return 60;
		}
	}
	
	// TODO: Use addCards method or let this be private and let the addCards call this method for each card in the collection.
	// Adds a single card on the pile
	public void addCard(Card card) 
	{
		if(pileType == PileType.CARDS_POSITION_BELOW)
			card.setLocation(this.m_dStartX, this.m_dStartY+13+(cards.size()*30));
		else
			card.setLocation(this.m_dStartX, this.m_dStartY+13);
			
		cards.add(card);
		
	}
	
	public void removeCard (Card card)
	{
		cards.remove(card);
	}
	
	public void removeAll ()
	{
		cards.clear();
	}
	
	// Add a collection of cards to this pile
	public void addCards(CardCollection cards)
	{
		for(Card c : cards)
		{
			addCard(c);
		}
	}
	
	// Removes a collection of card from this pile
	public void removeCards(CardCollection cards)
	{
		for(Card c : cards)
		{
			removeCard(c);
		}
	}
	
	public String toString()
	{
		return "x = " + m_dStartX + " y = " + m_dStartY;
	}
	
	public Pile findPile(Point2D point)
	{
		if (this.contains(point))
		{
			return this;
		}			
		return null;
	}
	
	// Returns a card object if the given point is where the card is positioned (use card contains method)
	public Card findCard(Point2D point)
	{
		Card result = null;
		for (Card c : this.getCards())
		{
			if (c.contains(point)) 
			{ 
				result = c; 
			}
		}
		return result;
	}
	
	public CardCollection findCardCollection(Point2D point)
	{
		boolean trailMe = false;
		Card card = null;
		CardCollection result = null;
		for (Card c : this.getCards())
		{
			if (c.contains(point)) 
			{ 
				card = c;
			}
		}
		if(card != null)
		{
			result = new CardCollection();
			for (Card c : this.getCards())
			{
				if(card == c || trailMe)
				{
					trailMe = true;
					result.add(c);
				}
			}
			trailMe = false;
		}		
		return result;
	}
}
